from typing import Any

from pydantic import Field, BaseModel


class ApiResponse(BaseModel):
    code: int = Field(200, example=200)
    message: str = Field("success", example="操作成功")
    isSuccess: bool = Field(True, example=True)
    data: object =  Field(None, example="响应数据")

    @classmethod
    def success(cls, data: Any = None) -> "ApiResponse":
        return cls(code=200, message="success", data=data)

    @classmethod
    def error(
            cls,
            data: Any = None,
            code: int = 500,
            message: str = "error"
    ) -> "ApiResponse":
        return cls(code=code, message=message,isSuccess=False, data=data)

